package com.aim.controller.system;

import com.aim.common.annotion.Permission;
import com.aim.common.base.AjaxResult;
import com.aim.common.entity.ZTreeNode;
import com.aim.common.service.ZtreeNodeService;
import com.aim.common.util.*;
import com.aim.config.shiro.ShiroKit;
import com.aim.entity.system.SysDict;
import com.aim.service.system.SysDictService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @AUTO 部门控制器
 * @Author AIM
 * @DATE 2019/4/26
 */
@Controller
@RequestMapping("/dict")
public class SysDictController {

    private static String PREFIX = "system/dict/";

    @Autowired
    private SysDictService dictService;
    @Autowired
    private ZtreeNodeService ztreeNodeService;

    /**
     * 列表页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "list.html";
    }

    /**
     * 查询列表
     */
    @PostMapping("/list")
    @Permission
    @ResponseBody
    public Object list(HttpServletRequest request) {
        Map<String, Object> params = WebUtil.getRequestMap(request);
        PageHelper.startPage(PageUtil.getPageNum(params), PageUtil.getPageSize(params));
        List<SysDict> list = dictService.findList(params);
        PageInfo<SysDict> pageInfo = new PageInfo<SysDict>(list);
        return AjaxResult.success((int) pageInfo.getTotal(), list);
    }

    /**
     * @AUTO 获取字典的tree列表
     * @DATE 2019/5/23
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public Object tree() {
        List<ZTreeNode> tree = this.ztreeNodeService.dictZtree();
        tree.add(ZTreeNode.createParent());
        return AjaxResult.success(tree);
    }

    /**
     * 添加编辑页面
     */
    @RequestMapping(value = {"add", "update"})
    public String editView(String id, Model model) {
        if (OftenUtil.isNotEmpty(id)) {
            SysDict sysDict = dictService.get(ConverterUtils.toLong(id));
            model.addAttribute("dict", sysDict);
        }
        return PREFIX + "edit.html";
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ResponseBody
    public Object save(SysDict dict) {
        SysDict sysDict = this.dictService.get(dict.getDictId());
        if (null == sysDict) {
            dict.setCreateTime(new Date());
            dict.setCreateUser(ShiroKit.getUser().getUserId());
            this.dictService.save(dict);
        } else {
            BeanUtils.copyProperties(dict, sysDict);
            sysDict.setUpdateTime(new Date());
            sysDict.setUpdateUser(ShiroKit.getUser().getUserId());
            this.dictService.update(sysDict);
        }
        return AjaxResult.success();
    }

    /**
     * 删除
     *
     * @param ids 删除ID集合
     */
    @Permission
    @PostMapping("/del")
    @ResponseBody
    public Object del(String ids) {
        if (OftenUtil.isNotEmpty(ids)) {
            this.dictService.batchDelete(ids.split(","));
            return AjaxResult.success();
        } else {
            return AjaxResult.error("参数异常");
        }
    }
}
